﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using AForge.Imaging.Filters;

namespace WpfImageDuplicates
{
    /// <summary>
    /// Interaction logic for FilterCollectionView.xaml
    /// </summary>
    public partial class FilterCollectionView : UserControl
    {
        public static readonly DependencyProperty FilterCollectionViewModelProperty =
            DependencyProperty.Register("FilterCollectionViewModel", typeof(FilterCollectionViewModel), typeof(FilterCollectionView), new PropertyMetadata(null, OnFilterCollectionViewModelPropertyChanged));

        public FilterCollectionViewModel FilterCollectionViewModel
        {
            get { return (FilterCollectionViewModel)GetValue(FilterCollectionViewModelProperty); }
            set { SetValue(FilterCollectionViewModelProperty, value); }
        }

        private static void OnFilterCollectionViewModelPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            FilterCollectionView source = d as FilterCollectionView;

            FilterCollectionViewModel v = e.NewValue as FilterCollectionViewModel;

            source.UpdateFilterCollectionViewModel(v);
        }

        private void UpdateFilterCollectionViewModel(FilterCollectionViewModel viewModel)
        {
            FilterCollectionViewModel = viewModel;
            this.DataContext = FilterCollectionViewModel;
        }

        public FilterCollectionView()
        {
            InitializeComponent();
        }

        public void AddFilter(Type type)
        {
            FilterCollectionViewModel.AddFilter(type);
        }

        private void btnAddFilter_Click(object sender, RoutedEventArgs e)
        {
            var types = showAvailable.GetSelectedTypes();

            foreach (var type in types)
            {
                FilterCollectionViewModel.AddFilter(type.Type);
            }

            showAvailable.ClearSelected();
        }

        private void btnClearFilters_Click(object sender, RoutedEventArgs e)
        {
            FilterCollectionViewModel.ClearAllFilters();
        }

        public void DeleteSelected()
        {
            List<FilterViewModel> toDelete = new List<FilterViewModel>();

            foreach (var item in lstItems.SelectedItems)
                toDelete.Add(item as FilterViewModel);

            FilterCollectionViewModel.RemoveFilters(toDelete);
        }

        private void lstItems_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Delete)
            {
                DeleteSelected();
            }
        }

        private void btnRemoveSelected_Click(object sender, RoutedEventArgs e)
        {
            DeleteSelected();
        }

        private void filterView_WantRemoveFilter(object sender, EventArgs e)
        {
            var fv = sender as FilterView;
            FilterCollectionViewModel.RemoveFilter(fv.FilterViewModel);
        }

        private void filterView_WantMoveLeft(object sender, EventArgs e)
        {
            var fv = sender as FilterView;
            FilterCollectionViewModel.MoveItemLeft(fv.FilterViewModel);
        }

        private void filterView_WantMoveRight(object sender, EventArgs e)
        {
            var fv = sender as FilterView;
            FilterCollectionViewModel.MoveItemRight(fv.FilterViewModel);
        }

        private void showAvailable_WantAdd(object sender, ObjectEventArgs<string> e)
        {
            FilterCollectionViewModel.AddFilter(e.obj);
        }        
    }
}
